﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using static RAP.Framework.Libary.DataSource.ServiceUtils;
using RAP.Framework.Libary.Utils;

namespace RAP.Framework.Libary.DataSource.Context
{
  /// <summary>
  /// 错误编码
  /// </summary>
  public class ServiceError
  {
    public int ErrorCode { get; set; }
    public string ErrorMessage { get; set; }
    public string DebugSql { get; set; }
    public object Param { get; set; }

    /// <summary>
    /// 错误的数据源类型
    /// </summary>
    public static ServiceError InvalidDataSource { get; } = new ServiceError { ErrorCode = 0x01, ErrorMessage = "无效的数据源：{0}" };

    public ServiceError FormatMessage( params string[] msgs )
    {
      this.ErrorMessage = this.ErrorMessage.format( msgs );
      return this;
    }

    public ServiceError( ) { }

    public ServiceError( IDictionary result )
    {
      if ( result != null )
      {
        this.ErrorCode = result.ToInt32( ERROR_CODE_FIELD );
        this.ErrorMessage = result.ToString( ERROR_MESSAGE_FIELD );
      }
    }

    public IDictionary ToDictionary( )
    {
      return new Hashtable
      {
          { "errorCode", this.ErrorCode } ,
          { "errorMessage", this.ErrorMessage }
      };
    }

    public override string ToString( )
    {
      return this.ToDictionary().ToJSON();
    }
  }

}
